using System;
using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb;
using Nsb.Classes;
using ThemeChain;
using jQueryApi;

namespace PlayNexus.Classes
{
    // ReSharper disable SuspiciousTypeConversion.Global
    // ReSharper disable PossibleInvalidCastException
    [IgnoreNamespace]
    public static class Build
    {
        public static string SendTo = (string) Window.LocalStorage.GetItem("DevEmailTo") ?? "natalie.kinsey@gmail.com";
        //public const string SendTo = "nsbawden@gmail.com";

        [PreserveCase, PreserveName]
        public static void ComePlay()
        {
            jQuery.Select(".SubmitEmail button")
                .Click(
                    delegate
                    {
                        Dictionary mail = new Dictionary();
                        SendMailData smd = (SendMailData) (object) mail;

                        smd.to = SendTo;
                        smd.subject = "Come Play Request!";
                        smd.from = jQuery.Select(".EmailFrom input[type=text]").Plugin<Jewl>().GetFValueString().Trim();
                        string content = "From: " + jQuery.Select(".EmailName input[type=text]").Plugin<Jewl>().GetFValueString().Trim() + "<br><br>";
                        smd.content = content + "<p>" + jQuery.Select(".EmailContent textarea").Plugin<Jewl>().GetFValueString().Trim() + "</p>";
                        SendTheMail(mail);
                    }
                )
                ;

            InitializePageInput();
        }

        [PreserveCase, PreserveName]
        public static void HomePage()
        {
            jQuery.Select(".SubmitRequest button")
                .Click(
                    delegate
                    {
                        Dictionary mail = new Dictionary();
                        SendMailData smd = (SendMailData) (object) mail;

                        smd.to = SendTo;
                        string from = jQuery.Select(".Email input[type=text]").Plugin<Jewl>().GetFValueString().Trim();
                        smd.subject = "PlayNexus - News Letter Request";
                        smd.from = from;
                        string content = "From: " + from + "<br>";
                        smd.content = content + "<p>Please add me to your news letter &#9786;</p>";
                        SendTheMail(mail);
                    }
                )
                ;

            InitializePageInput();
        }

        [PreserveCase, PreserveName]
        public static void ScheduleSalon()
        {
            jQuery.Select(".ScheduleSalon button")
                .Click(
                    delegate { new Ask().OkAw(Await.AsyncAw, "Feature coming soon.<br>Try again tomorrow! " + ThemeBase.Hearts); }
                )
                ;

            InitializePageInput();
        }

        private static void SendTheMail(Dictionary mail)
        {
            if ((bool) Storage.GetLocal("DevNoEmail"))
                return;
            new Await()
                .AddAw(CloudMail.SendMailAw, mail)
                .AddDx(delegate { Ask.Ok("Your request has been received.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Thank you!"); })
                .Commit();
        }

        private static void InitializePageInput()
        {
            jQueryObject gigs = jQuery.Select("#PageGigs");

            jQuery.Select("input[type=text],textarea", gigs)
                .Each(
                    delegate(int i, Element domEl)
                    {
                        jQueryObject ths = jQuery.FromElement(domEl);
                        ths.Attribute("title", ths.GetValue());
                        ths.Focus(delegate(jQueryEvent e)
                                  {
                                      jQueryObject el = jQuery.FromElement(e.Target);
                                      string title = el.GetAttribute("title");
                                      if (el.GetValue().Trim() == title)
                                          el.Value("");
                                  })
                            ;
                        ths.Blur(delegate(jQueryEvent e)
                                 {
                                     jQueryObject el = jQuery.FromElement(e.Target);
                                     if (String.IsNullOrEmpty(el.GetValue().Trim()))
                                         el.Value(el.GetAttribute("title"));
                                 })
                            ;
                    }
                )
                ;
        }
    }
}